// 用户登录
function login(){
			var $uname = uname.value;
			var $upwd = upwd.value;
			//console.log($uname,$upwd);
			// 非空验证
			if(!$uname || !$upwd ){d1.innerHTML = "用户或者密码不能为空"; return}

			var xhr = new XMLHttpRequest();
			
			xhr.onreadystatechange = function (){
				if(xhr.readyState == 4 && xhr.status == 200){
					var r = xhr.responseText;
					if(r==1){
						d1.innerHTML = "用户登录成功";
						location.href = "user_list.html";
					}else{							
						d1.innerHTML = "用户或者密码错误";
					}
				}
			}
			
			xhr.open('get',`/pro/v1/login/${$uname}&${$upwd}`,true);
			xhr.send();
		}
			
// 用户注册
function reg(){
	var $uname = uname.value;
		var $upwd1 = upwd1.value;
		var $upwd2 = upwd2.value;
		var $email = email.value;
		var $phone = phone.value;
		// 非空验证
		if (!$uname || !$upwd1 || !$email || !$phone){d1.innerHTML = '不能为空';return;}
		// 重复验证
		if(!$upwd2){
			d1.innerHTML = '错误不能为空';
			return 0;
		}else if( $upwd2 == $upwd1){
			d1.innerHTML = '√';
		}else{
			d1.innerHTML = '两次输入不一样';
			return 0;
		}
			
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
				var r = xhr.responseText;
				if(r==1){
					d2.innerHTML = "用户注册成功";
					d1.innerHTML = "三秒之后跳转登录页面";
					setTimeout(function(){
						// 跳转到登录窗口
						location.href = 'uesr_login.html'
					},3000)
					
					//list();
				}else{							
					d2.innerHTML = "用户注册失败";
				}
			}
		}
		
		xhr.open('post','/pro/v1/reg',true);
		xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		var fromdata = `uname=${$uname}&upwd=${$upwd1}&email=${$email}&phone=${$phone}`
		xhr.send(fromdata);		
	}
	
// 号码查重
function isPhone(){
		var $phone = phone.value;
		
		var xhr = new XMLHttpRequest();
			
			xhr.onreadystatechange = function (){
				if(xhr.readyState == 4 && xhr.status == 200){
					var r = xhr.responseText;
					if(r==1){
						d1.innerHTML = "号码已存在";
						return;
					}else{							
						d1.innerHTML = "号码可用";
					}
				}
			}
			xhr.open('get',`/pro/v1/checkphone/${$phone}`,true);
			xhr.send();
	}

// 分页查询用户
function list(a,b){
	var $page = a;
	var $count = b;
	
	if(!$page || isNaN($page) || $page < 1){ $page = 1}
	if(!$count || isNaN($count) || $count < 0){ $count = 5}
	
	$page = parseInt( ($page - 1) * $count );

	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = function(){
	if(xhr.readyState == 4 && xhr.status == 200){
		var r = xhr.responseText;
		var arr = JSON.parse(r);
		var strHTML =`
			<table border="1" cellspacing="0" cellpadding="0" id="tb" >
			<tr>
				<th width="100px">用户编号</th>
				<th width="140px">用户名</th>
				<th width="140px">密码</th>
				<th width="140px">邮箱</th>
				<th width="140px">电话</th>
				<th width="100px">头像</th>
				<th width="100px">名字</th>
				<th width="100px">性别</th>
				<th width="100px">操作</th>
			</tr>
		`;
			for(var i=0;i<arr.length;i++){
				if(arr[i].gender == 0){ arr[i].gender = '女' }
				else if(arr[i].gender == 1){ arr[i].gender = '男' }
				else if(arr[i].gender == null){ arr[i].gender = '不详' }
				strHTML+=`
					<tr>
						<td width="100px">${arr[i].uid}</td>
						<td width="100px">${arr[i].uname}</td>
						<td width="100px">${arr[i].upwd}</td>
						<td width="100px">${arr[i].email}</td>
						<td width="100px">${arr[i].phone}</td>
						<td width="100px">${arr[i].avatar}</td>
						<td width="100px">${arr[i].user_name}</td>
						<td width="100px">${arr[i].gender}</td>
						<td width="100px">
						<a href="user_search.html?uid=${arr[i].uid}">修改</a>
						<a href="javascript:del(${arr[i].uid})">删除</a>	
						</td>
					</tr>
				`
			}
			strHTML+='</table>';
		d1.innerHTML = strHTML;
		
	}		
}

xhr.open("get",`/pro/v1/list/${$page}&${$count}`);
xhr.send();


}

// 删除用户信息
function del($uid){
	//var $uid = uid.value;
	//console.log($uname,$upwd);
	
	var xhr = new XMLHttpRequest();
	
	xhr.onreadystatechange = function (){
		if(xhr.readyState == 4 && xhr.status == 200){
			var r = xhr.responseText;
			if(r==1){
				d2.innerHTML = "用户删除成功";
				list();
			}else{							
				d2.innerHTML = "用户删除失败";
			}
		}
	}
	
	xhr.open('delete',`/pro/v1/del/${$uid}`,true);
	xhr.send();
}

// 用单独查询返回到修改窗口
function search(){
	var obj = new URLSearchParams(location.search);
	var $uid = obj.get('uid');
		console.log($uid)
	var xhr = new XMLHttpRequest();
	
	xhr.onreadystatechange = function (){
		if(xhr.readyState == 4 && xhr.status == 200){
			var r = xhr.responseText;
			if(r==0){
				alert('不存在')
			}else{
			var arr = JSON.parse(r);
                     console.log(r)
				uname.value = arr[0].uname;
				email.value = arr[0].email;
				phone.value = arr[0].phone;
				user_name.value = arr[0].user_name;
				if(arr[0].gender ==1){
					man.checked =true;
				}else if(arr[0].gender ==0){
					wm.checked =true;
				}else{
					man.checked =true;
				}
			}

		}
	}
	
	xhr.open('get',"/pro/v1/search/"+$uid,true);
	xhr.send();
}

// 用户修改
function update(){
	var obj = new URLSearchParams(location.search);
	var $uid = obj.get('uid');
	var $uname = uname.value;
	var $email = email.value;
	var $phone = phone.value;
	var $user_name = user_name.value;
	var $gender="-1";
		if(man.checked){
			$gender = "1";
		}else if(wm.checked){
			$gender = "0";
		}else{
			$gender = "null";
		}
	//console.log($uname,$upwd);
			var xhr = new XMLHttpRequest();
	
	xhr.onreadystatechange = function (){
		if(xhr.readyState == 4 && xhr.status == 200){
			var r = xhr.responseText;
			if(r==1){
				d1.innerHTML = "用户修改成功";
				// 自动跳转新页面
				location.href = "user_list.html";
			}else{							
				d1.innerHTML = "用户修改失败";
			}
		}
	}
	  console.log($uid);
				    
	xhr.open("put","/pro/v1/update",true);
	xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	var formdata = `uid=${$uid}&uname=${$uname}&email=${$email}&phone=${$phone}&user_name=${$user_name}&gender=${$gender}`;
	console.log(formdata);
	xhr.send(formdata);

} 
			
			
			
			